package com.zjy.controller;

import com.zjy.bean.*;
import com.zjy.dao.ArticleTagMapper;
import com.zjy.dto.*;
import com.zjy.service.GoodsDetailService;
import com.zjy.service.GoodsPhotoService;
import com.zjy.service.GoodsService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/4/11.
 */
@CrossOrigin
@Controller
@RequestMapping(value = "/goods")
public class GoodsController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsPhotoService goodsPhotoService;
    @Resource
    private GoodsDetailService goodsDetailService;
    @Resource
    private ArticleTagMapper articleTagMapper;

    /**
     * 新增商品（只包括商品）
     * @param goods
     * @return
     */
    @RequestMapping(value = "/addGoods",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoods(@RequestBody GoodsDTO goods){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goods.getChineseName())){
            return result.error(CLIENT_ERROR,"商品中文名不能为空！");
        }else {
            try{
                String goodsId = goodsService.addGoods(goods);
                result.setMsg("新增商品成功！");
                result.setData(goodsId);
            }catch (Exception e){
                return result.error(SERVER_ERROR,"新增商品失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 新增商品图片（只包括商品图片）
     * @param goodsPhoto
     * @return
     */
    @RequestMapping(value = "/addGoodsPhoto",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoodsPhoto(@RequestBody GoodsPhoto goodsPhoto){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsPhoto.getGoodsId())){
            return result.error(CLIENT_ERROR,"goodsId不能为空！");
        }else {
            try{
                goodsPhotoService.addGoodsPhoto(goodsPhoto);
                result.setMsg("新增商品图片成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,"新增商品图片失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 新增商品详情（只包括商品详情）
     * @param goodsDetail
     * @return
     */
    @RequestMapping(value = "/addGoodsDetail",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoodsDetail(@RequestBody GoodsDetail goodsDetail){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsDetail.getGoodsId())){
            return result.error(CLIENT_ERROR,"goodsId不能为空！");
        }else {
            try{
                goodsDetailService.addGoodsDetail(goodsDetail);
                result.setMsg("新增商品详情成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,"新增商品详情失败，原因："+ e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据Id软删除商品(包括商品、商品图片、商品详情)
     * @param goods
     * @return
     */
    @RequestMapping(value = "/deleteGoodsById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteGoodsById(@RequestBody Goods goods){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goods.getId())){
            return result.error(CLIENT_ERROR,"参数id不能为空！");
        }else {
            try{
                int count = goodsService.deleteGoodsById(goods.getId());
                if(count == 0){
                    return result.error(CLIENT_ERROR,"找不到此id对应的商品");
                }else if(count == 1){
                    result.setMsg("商品删除成功！");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"商品删除失败，原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据Id更新商品（只包括商品）
     * @param goods
     * @return
     */
    @RequestMapping(value = "/updateGoodsById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateGoodsById(@RequestBody GoodsDTO goods){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goods.getId())){
            return result.error(CLIENT_ERROR,"参数id不能为空！");
        }else {
            try{
                int count = goodsService.updateGoodsById(goods);
                if(count == 0){
                    return result.error(CLIENT_ERROR,"找不到此id对应的商品");
                }else if(count == 1){
                    result.setMsg("商品更新成功！");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"商品更新失败，原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据Id更新商品图片（只包括商品图片）
     * @param goodsPhoto
     * @return
     */
    @RequestMapping(value = "/updateGoodsPhotoById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateGoodsPhotoById(@RequestBody GoodsPhoto goodsPhoto){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsPhoto.getId())){
            return result.error(CLIENT_ERROR,"参数id不能为空！");
        }else {
            try{
                int count = goodsPhotoService.updateGoodsPhotoById(goodsPhoto);
                if(count == 0){
                    return result.error(CLIENT_ERROR,"找不到此id对应的商品图片");
                }else if(count == 1){
                    result.setMsg("商品图片更新成功！");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"商品图片更新失败，原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据Id更新商品详情（只包括商品详情）
     * @param goodsDetail
     * @return
     */
    @RequestMapping(value = "/updateGoodsDetailById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateGoodsDetailById(@RequestBody GoodsDetail goodsDetail){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsDetail.getId())){
            return result.error(CLIENT_ERROR,"参数id不能为空！");
        }else {
            try{
                int count = goodsDetailService.updateGoodsDetailById(goodsDetail);
                if(count == 0){
                    return result.error(CLIENT_ERROR,"找不到此id对应的商品详情");
                }else if(count == 1){
                    result.setMsg("商品详情更新成功！");
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"商品详情更新失败，原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 根据Id查询商品(包括商品、商品标签、商品图片、商品详情)
     * @param param
     * @return
     */
    @RequestMapping(value = "/selectGoodsById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectGoodsById(@RequestBody GoodsParam param){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(param.getGoodsId())){
            return result.error(CLIENT_ERROR,"参数id不能为空！");
        }else {
            try{
                GoodsResponse goodsResponse = new GoodsResponse();
                Goods goods1 = goodsService.selectGoodsById(param.getUserId(),param.getGoodsId());//查询商品
                List<ArticleTag> goodsTagList = articleTagMapper.selectArticleTagByArticleId(param.getGoodsId());//查询商品标签
                GoodsPhoto goodsPhoto = goodsPhotoService.selectByGoodsId(param.getGoodsId());//查询商品图片
                GoodsDetail goodsDetail = goodsDetailService.selectByGoodsId(param.getGoodsId());//查询商品详情
                goodsResponse.setGoods(goods1);
                goodsResponse.setGoodsDetail(goodsDetail);
                goodsResponse.setGoodsPhoto(goodsPhoto);
                goodsResponse.setGoodsTagList(goodsTagList);
                if(ObjectUtils.isNullOrEmpty(goods1)){
                    return result.error(CLIENT_ERROR,"找不到此id对应的商品");
                }else{
                    result.setMsg("商品查询成功！");
                    result.setData(goodsResponse);
                }
            }catch (Exception e){
                return result.error(SERVER_ERROR,"商品查询失败，原因："+e.getMessage());
            }
        }
        return result;
    }

    /**
     * 首页：根据筛选条件，查询上架商品列表(只包含商品)
     * @return
     */
    @RequestMapping(value = "/selectGoodsList",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectGoodsList(@RequestBody GoodsParam param){
        ApiResult result = new ApiResult();
        try{
            ListResponse response = goodsService.selectGoodsList(param);//获取符合条件的已上架商品列表
            if(ObjectUtils.isNullOrEmpty(response.getData())){
                result.setMsg("商品列表为空！");
            }else{
                result.setMsg("商品列表查询成功！");
                result.setData(response);
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,"商品列表查询失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 获取商品总数、已上架商品总数、库存小于10的商品数量
     * @return
     */
    @RequestMapping(value = "/getGoodsData",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getGoodsData(){
        ApiResult result = new ApiResult();
        try{
            HashMap<String,Object> map = goodsService.getGoodsData();
            result.setMsg("商品数量查询成功！");
            result.setData(map);
        }catch (Exception e){
            return result.error(SERVER_ERROR,"商品数量查询失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 收藏商品
     * @return
     */
    @RequestMapping(value = "/addGoodsCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addGoodsCollection(@RequestBody UserCollection userCollection){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(userCollection.getTargetId()) || ObjectUtils.isNullOrEmpty(userCollection.getUserId())){
            return result.error(CLIENT_ERROR,"targetId和userId不能为空！");
        }
        try{
            int count = goodsService.addGoodsCollection(userCollection);
            if(count == 1){
                result.setMsg("收藏商品成功！");
            }else if(count == 0){
                result.error(CLIENT_ERROR,"收藏商品失败！没有找到此targetId对应的商品");
            }

        }catch (Exception e){
            return result.error(SERVER_ERROR,"收藏商品失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 取消收藏商品
     * @return
     */
    @RequestMapping(value = "/cancelGoodsCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult cancelGoodsCollection(@RequestBody UserCollection userCollection){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(userCollection.getTargetId()) || ObjectUtils.isNullOrEmpty(userCollection.getUserId())){
            return result.error(CLIENT_ERROR,"targetId和userId不能为空！");
        }
        try{
            int count = goodsService.cancelGoodsCollection(userCollection);
            if(count == 1){
                result.setMsg("取消收藏商品成功！");
            }else if(count == 0){
                result.error(CLIENT_ERROR,"取消收藏商品失败！没有找到此targetId对应的商品");
            }

        }catch (Exception e){
            return result.error(SERVER_ERROR,"取消收藏商品失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 获取用户收藏商品列表
     * @return
     */
    @RequestMapping(value = "/getGoodsCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getGoodsCollection(@RequestBody GoodsParam goodsParam){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(goodsParam.getUserId())){
            return result.error(CLIENT_ERROR,"userId不能为空！");
        }
        try{
            ListResponse response = goodsService.getGoodsCollection(goodsParam);
            if(ObjectUtils.isNullOrEmpty(response.getData())){
                result.setMsg("商品收藏列表为空！");
            }else{
                result.setMsg("商品收藏列表查询成功！");
                result.setData(response);
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,"商品收藏列表查询失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 批量删除用户商品收藏
     * @return
     */
    @RequestMapping(value = "/deleteGoodsCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteGoodsCollection(@RequestBody HashMap<String,Object> param){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(param.get("userId"))){
            return result.error(CLIENT_ERROR,"userId不能为空！");
        }
        try{
            int count = goodsService.deleteGoodsCollection(param);
            if(count == 0){
                result.setMsg("找不到该收藏关系！");
            }else if(count > 0){
                result.setMsg("批量删除商品收藏成功！");
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,"批量删除商品收藏失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 获取一款限时折扣商品和8款推荐商品
     * @return
     */
    @RequestMapping(value = "/getNineGoods",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getNineGoods(){
        ApiResult result = new ApiResult();
        try{
            HashMap<String,Object> map = goodsService.getNineGoods();
            result.setData(map);
            result.setMsg("查询成功！");
        }catch (Exception e){
            return result.error(SERVER_ERROR,"查询失败，原因："+e.getMessage());
        }
        return result;
    }

}
